<?php
namespace application\admin\panels;

use application\utilities\Utility;
class content_relations extends panel {
	public $name = 'Content Relations';
	
	public function title() {
		return 'Content Relations';
	}
	
	public function sidebar() {
		$contentTypes = DB()->getResults("SELECT `ct_plural`, `ct_id` FROM `cn_content_types` ORDER BY `ct_title`");
		$returnArray = array();
		
		foreach($contentTypes as $type) {
			$returnArray[] = array('title'=>$type->ct_plural, 'id'=>$type->ct_id);
		}
		
		return $returnArray;
	}
	
	public function handleData() {
		global $concerto;
		if($_REQUEST['delete_relation']) {
			DB()->query("DELETE FROM `cn_content_relation_types` WHERE `crt_id`='".$_REQUEST['crt_id']."'");
			DB()->query("DELETE FROM `cn_content_content_relations` WHERE `ccr_relation`='".$_REQUEST['crt_id']."'");
		}
		else {
			var_dump($_REQUEST['relations']);
			foreach($_REQUEST['relations'] as $relation) {
				$insert = array('crt_type1'=>$relation['relation_type1'], 'crt_type2'=>$relation['relation_type2'], 'crt_type3'=>$relation['relation_type3'], 'crt_name'=>$relation['crt_name'], 'crt_relation_type'=>$relation['crt_relation_type']);
				if($relation['crt_id']) {
					DB()->update('cn_content_relation_types', 'crt_id', $relation['crt_id'], $insert);
				}
				else {
					DB()->insert('cn_content_relation_types', $insert);
				}
			}
		}
	}

	
	
	
	public function display() {
		global $concerto;
		
		?>
		<script type="text/javascript">
			$(document).ready(function() {
				$( "ul.content-relation-ul" ).droppable({
					drop:function(event, ui) {
						if($(this).children('li').length < 3) {
							ui.helper.clone().attr('class', '').attr('style', '').append('<button class="remove-relation-type">X</button>').appendTo($(this));
						}
						$(this).parent('li').removeClass('droppable-hover').removeClass('droppable-hover-nodrop');
					},
					over:function() {
						if($(this).children('li').length > 2) {
							$(this).parent('li').addClass('droppable-hover-nodrop');
						}
						$(this).parent('li').addClass('droppable-hover');
					},
					out:function() {
						$(this).parent('li').removeClass('droppable-hover').removeClass('droppable-hover-nodrop');
					}
				});
				
				$('.remove-relation-type').live('click', function(e) {
					e.preventDefault();
					$(this).parent('li').remove();
				});
				$('.relation-title-edit').live('click', function(e) {
					var h4 = $(this).parent('h4');
					e.preventDefault();
					torchd.ui.showDialog({
						type:'prompt',
						title:'Edit Relation Name',
						defaultValue:h4.children('span').html(),
						onSubmit:function(data) {
							h4.children('span').html(data[0].val);
						}
					});
				});
				$('.relation-remove').live('click', function(e) {
					var id = $(this).parent('h4').parent('li').attr('data-id');
					var relation = $(this).parent('h4').parent('li');
					e.preventDefault();
					torchd.ui.showDialog({
						type:'confirm',
						title:'Delete Relation?',
						message:'Are you sure you want to delete this relation?',
						onSubmit:function() {
							if(id) {
								$.ajax({
									url:concerto.admin.rootUrl + 'admin/?display=content_relations',
									type:'POST',
									beforeSend:function() {
										torchd.ui.showDialog({
											type:'ajax-loader',
											title:'Deleting...'
											
										});
									},
									data:{
										delete_relation:1,
										crt_id:id
									},
									success:function() {
										torchd.ui.closeDialog('Deleting...');
										relation.fadeOut(200, function() {
											$(this).remove();
										});
									}
								});
							}
							else {
								relation.fadeOut(200, function() {
									$(this).remove();
								});
							}
						}
					});
				});
				$('#new-relation').click(function(e) {
					e.preventDefault();
					torchd.ui.showDialog({
						type:'prompt',
						title:'New Relation',
						message:'What do you want to call this relation?',
						defaultValue:'',
						onSubmit:function(response) {
							var li = $('<li/>').html('<h4><span>' + response[0].val + '</span><button class="relation-remove"></button><button class="relation-title-edit"></button></h4>').appendTo($('#content-relations'));
							
							
							$('<ul/>').addClass('content-relation-ul').droppable({
								drop:function(event, ui) {
									if($(this).children('li').length < 3) {
										ui.helper.clone().attr('class', '').attr('style', '').append('<button class="remove-relation-type">X</button>').appendTo($(this));
									}
									$(this).parent('li').removeClass('droppable-hover').removeClass('droppable-hover-nodrop');
								},
								over:function() {
									if($(this).children('li').length > 2) {
										$(this).parent('li').addClass('droppable-hover-nodrop');
									}
									$(this).parent('li').addClass('droppable-hover');
								},
								out:function() {
									$(this).parent('li').removeClass('droppable-hover').removeClass('droppable-hover-nodrop');
								}
							}).appendTo(li);
						}
					});
					
				});
			});
		</script>
		<div class="box">
			<h3><a class="tooltip question-mark">?</a><div class="tooltip-content">
				<div class="tooltip-arrow"></div>
				<div class="tooltip-inner">
				<h3>Content Relations</h3>
				Content relations allow you to create basic connections between your content that don't fit in to a parent-child relationship. (You can set options for parent-child relationships on the configuration panel for each content type.) 
				<br /><br />
				For example, you can create a relationship for Movies-Actors-Characters, which would allow you to say, for instance, that the actor John Travolta played the character Chili Palmer in the movie <i>Get Shorty</i>.
				<br /><br />
				Note that to be able to set content relationships to in the form for adding and editing content, you must add the Linear Content Relation or Triangular Content Relation data type to that particular content type's data fields.
				</div>
			</div>Content Relations</h3>
			<div class="box-content">
				<h5>Drag content types from the left into the relation boxes. You can have either two (linear) or three (triangular) content types per relation.</h5>
				<button id="new-relation" class="small-button">New Relation</button>
				
				<?php $relations = DB()->getResults("SELECT *, `a`.`ct_plural` AS `type1_name`, `a`.`ct_id` as `type1_id`, `b`.`ct_plural` AS `type2_name`, `b`.`ct_id` as `type2_id`, `c`.`ct_plural` AS `type3_name`, `c`.`ct_id` as `type3_id` FROM `cn_content_relation_types` INNER JOIN `cn_content_types` `a`  ON `a`.`ct_id`=`crt_type1` INNER JOIN `cn_content_types` `b` ON `b`.`ct_id`=`crt_type2` LEFT JOIN `cn_content_types` `c` ON `c`.`ct_id`=`crt_type3`");?>
				<ul id="content-relations">
				<?php foreach($relations as $relation) : ?>
					<li data-id="<?=$relation->crt_id?>">
						<h4><span><?=$relation->crt_name?></span><button class="relation-remove"></button><button class="relation-title-edit"></button></h4>
						<ul class="content-relation-ul">
							<li data-id="<?=$relation->type1_id?>"><?=$relation->type1_name?><button class="remove-relation-type">X</button></li>
							<li data-id="<?=$relation->type2_id?>"><?=$relation->type2_name?><button class="remove-relation-type">X</button></li>
							<?php if($relation->type3_id) : ?>
							<li data-id="<?=$relation->type3_id?>"><?=$relation->type3_name?><button class="remove-relation-type">X</button></li>
							<?php endif;?>
						</ul>
					</li>
				<?php endforeach;?>
				</ul>
				
					<div class="clearboth"></div>
			</div>
		</div>
		
		<?php
	}
}
?>